package com.enterprise.support.utility.generator.panel;

import com.enterprise.application.swing.jeta.AbstractPanel;
import com.enterprise.support.utility.SwingUtility;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.io.File;
import java.io.FileFilter;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-1-31
 * Time: 14:49:41
 * To change this template use File | Settings | File Templates.
 */
public class TabPanel extends AbstractPanel {
    private final static File srcDirectory = new File(com.enterprise.support.utility.Configuration.LibDirectory, "/java/src");
    private final static File comDirectory = new File(srcDirectory, "com");


    public TabPanel() {
        super("com\\enterprise\\support\\utility\\generator\\panel\\TabPanel.xml");
    }

    public void initComponents() {
        super.mainPanel.getTabbedPane("tabs").addTab("Project Settings ", new TabProject());
        super.mainPanel.getTabbedPane("tabs").addTab("Hiberante ", new TabHiberante());
        super.mainPanel.getTabbedPane("tabs").addTab("Spring ", new TabSpring());
        super.mainPanel.getTabbedPane("tabs").addTab("Struts ", new TabStruts());
    }

    @Override
    public void addListeners() {
        super.mainPanel.getTabbedPane("tabs").addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent ex) {
                AbstractPanel panel = (AbstractPanel) mainPanel.getTabbedPane("tabs").getSelectedComponent();
                panel.initComponents();
                if (mainPanel.getTabbedPane("tabs").getSelectedIndex() > 0) {
                    boolean flg = false;
                    File[] projects = comDirectory.listFiles(new FileFilter() {
                        public boolean accept(File file) {
                            if (file.isDirectory()) {
                                File[] files = new File(file, "application").listFiles(new FileFilter() {
                                    public boolean accept(File file) {
                                        return file.isDirectory();
                                    }
                                });
                                if (files != null) {
                                    for (File module : files) {
                                        return new File(module, "domain").exists();
                                    }
                                }
                            }
                            return false;
                        }
                    });
                    if (null != projects && projects.length > 0) {
                        SwingUtility.enable(mainPanel.getTabbedPane("tabs").getTopLevelAncestor());
                    } else {
                        SwingUtility.disable((Container) mainPanel.getTabbedPane("tabs").getSelectedComponent());
                    }
                }
            }
        });
    }
}
